//JavaScript代码区域
layui.use(['layer', 'form', 'table'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		table = layui.table;
	//数据表格实例
	table.render({
		id: 'idTest',
		elem: '#demo',
		url: IP+'/api/teacher_eav/quartz/queryjob',
		height: 'full-180',
		method: 'get',
		page: true,
		even: true,
		cols: [[
		   {field: 'id',align:'center', title: 'id', hide: true}
	      ,{field: 'jobName',align:'center', title: '任务名称', sort: true}
	      ,{field: 'jobDesc',align:'center', title: '任务描述', sort: true}
	      ,{field: 'jobClassName', align:'center',title: '任务类名', sort: true}
	      ,{field: 'jobGroupName', align:'center',title: '任务所在组', sort: true}
	      ,{field: 'cron', align:'center',title: '表达式', sort: true}
	      ,{field: 'status', align:'center',title: '状态', sort: true}
	      ,{title: '操作', align:'center', toolbar: '#barDemo'}
	    ]],
	    request: {
		  pageName: 'pageNum'
		  ,limitName: 'pageSize'
		},              
	    response:{
	    	  statusName: 'code'
			  ,statusCode: 200
			  ,msgName: 'msg'
			  ,countName: 'count'
			  ,dataName: 'data'
	    },
	    done:function(res, curr, count){
			layer.closeAll('loading');
	    },
	    parseData: function(res){
		    return parseResultData(res);
	  	}
	});
	function parseResultData(res){
		var data=res.data;
		var paseData=[];
		$.each(data, function(idx,obj) {
			var id=obj.id;
			var jobClassName=obj.jobClassName;
			var jobGroupName=obj.jobGroupName;
			if(obj.status==0){
				obj.status='<input id="'+id+'" jobClassName="'+jobClassName+'" jobGroupName="'+jobGroupName+'" lay-filter="jobcheckbox" type="checkbox" lay-skin="switch" lay-text="运行中|已暂停" checked>'
			}else if(obj.status==1){
				obj.status='<input id="'+id+'" jobClassName="'+jobClassName+'" jobGroupName="'+jobGroupName+'" lay-filter="jobcheckbox" type="checkbox" lay-skin="switch" lay-text="运行中|已暂停">'
			}
			paseData.push(obj)
		});
		return {
	      "code": res.code,
	      "msg": res.message,
	      "count": res.count,
	      "data": paseData
	    };
	}
	//数据表格监听工具条(查看、编辑、删除按钮)
	table.on('tool(test)', function(obj){
	  var data = obj.data;
	  var layEvent = obj.event;
	  if(layEvent === 'start'){
	  	console.log(data)
	  }else if(layEvent === 'del'){
		  deleteByUids(data.id,data.jobClassName,data.jobGroupName);
	  } else if(layEvent === 'edit'){
	    window.location="quartz_update.html?id="+data.id;	    
	  }
	});
	$(function() {
		//添加按钮事件
		$('#adduser').on('click',function(){
			window.location='quartz_add.html?type=add'
		})
	})
	//单、批量删除
	function deleteByUids(id,jobClassName,jobGroupName){
		layer.confirm('真的删除'+jobClassName+"吗？",{icon:3}, function(index) {
			layer.close(index);
			$.ajax({
				url: IP + '/api/teacher_eav/quartz/deletejob',
				type: "POST",
				data: JSON.stringify({
					id:id,
					jobClassName:jobClassName,
					jobGroupName:jobGroupName
				},),
				success: function(result, status, xhr) {
					layer.closeAll('loading');
					layer.msg("操作成功", {
						icon: 1,
						time: 1000
					},function(){
						table.reload('idTest', {});
					});
				}
			});
		});
	}
	//任务开关
	form.on('switch(jobcheckbox)', function(data){
		var obj=data.elem;
		var id=obj.getAttribute("id");
		var jobclassname=obj.getAttribute("jobclassname");
		var jobgroupname=obj.getAttribute("jobgroupname");
		var _data=JSON.stringify({
			id:id,
			jobClassName:jobclassname,
			jobGroupName:jobgroupname
		})
		var url=obj.checked?'resumejob':'pausejob';
		obj.setAttribute('disabled','disabled');
		form.render();
		changeJobStatus(url,_data,obj);
	});
	
	function changeJobStatus(url,data,obj){
		jQuery.ajax({
			url: IP + '/api/teacher_eav/quartz/'+url,
			type: 'POST',
			data: data,
			async: false,
			success: function(result, status, xhr) {
				layer.closeAll('loading');
				layer.msg(result.message, {
					icon: 1,
					time: 1000
				},function(){
					obj.removeAttribute('disabled','disabled');
					form.render();
				});
			}
		});
	}
});